package com.zsd.android.recyclerviewlibrary.holder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RViewHolder extends RecyclerView.ViewHolder {

    private SparseArrayCompat<View> mViews;
    private View mCurrentView;

    public RViewHolder(@NonNull View itemView) {
        super(itemView);
        mCurrentView = itemView;
        mViews = new SparseArrayCompat<>();
    }

    public static RViewHolder create(Context mContext, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
        return new RViewHolder(itemView);
    }

    public View getCurrentView() {
        return mCurrentView;
    }

    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mCurrentView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}
